<?php


namespace app\services\utils;


class AmapService
{
    protected $key;

    public function __construct()
    {
        $this->key = config('amap.key');
    }

    /**
     * 根据地址获取经纬度
     * @param $address
     * @return array|bool
     */
    public function getCoordinate($address)
    {
        $url = "https://restapi.amap.com/v3/geocode/geo?address=$address&output=JSON&key=" . $this->key;
        $result = curl_get($url);
        $arr = json_decode($result, true);
        if ($arr['status'] != 1 || $arr['infocode'] != 10000) {
            return false;
        }
        $location = $arr['geocodes'][0]['location'];
        $array = explode(',', $location);
        return ['lng' => $array[0], 'lat' => $array[1]];
    }
}